<template>
  <div class="login-container">
    <van-nav-bar title="委托待售商品列表" left-text="" right-text="" left-arrow @click-left="onClickLeft">
      <template #left>
        <van-icon name="cross" size="19" color="#000" />
      </template>
      <!-- <template #right>
        <van-icon name="ellipsis" size="22" color="#000" />
      </template> -->
    </van-nav-bar>
    <div class="header">
      <img class="header__logo" src="@/assets/Group 9458.png" alt="logo">
      <div style="font-family: PingFang SC, PingFang SC;font-weight: 500;font-size: 13px;color: #666666;">
        未注册的手机号将自动注册并登录
      </div>
    </div>

    <van-form class="form" @submit="onSubmit">
      <div style="display: flex;margin-top: 50px;">
        <img src="@/assets/Frame.png" alt="logo" style="width: 22px;height: 22px;margin-left: 26px;">
        <div
          style="font-family: MiSans, MiSans;font-weight: 400;font-size: 15px;color: #000000;margin-left: 9px;margin-top: 3px;">
          手机号
        </div>
      </div>
      <van-field v-model="form.mobile" type="text" clearable placeholder="请输入手机号码"
        style="width: 325px;height:44px;background-color: #EFEFEF;border-radius: 77px;margin-top: 10px;margin-left: 25px;" />

      <!-- <van-field v-model="form.password" type="password" required center clearable name="密码" label="密码"
        placeholder="请输入密码" :rules="[{ required: true }]" /> -->
      <div style="display: flex;margin-top: 23px;">
        <img src="@/assets/images/Frame.png" alt="logo" style="width: 22px;height: 22px;margin-left: 26px;">
        <div
          style="font-family: MiSans, MiSans;font-weight: 400;font-size: 15px;color: #000000;margin-left: 9px;margin-top: 3px">
          验证码
        </div>
      </div>
      <van-field v-model="form.captcha" type="text" clearable placeholder="请输入验证码"
        style="width: 325px;height:44px;background-color: #EFEFEF;border-radius: 77px;margin-top: 10px;margin-left: 25px;">
        <template #button>
          <div v-if="isclick == 1" style="font-family: MiSans, MiSans;font-weight: 400;font-size: 15px;color: #2E512E;"
            @click="getCaptcha">
            {{ message }}</div>
          <div v-else-if="isclick == 2"
            style="font-family: MiSans, MiSans;font-weight: 400;font-size: 15px;color: #2E512E;">
            倒计{{ seconds }}秒</div>
        </template>
      </van-field>

      <div style="margin: 36px">
        <van-button round block :loading="loading" type="primary" loading-text="登录中..." native-type="submit"
          style="background-color: #389138;">立即登录</van-button>
      </div>
    </van-form>
    <div style="text-align: center;margin-top: 119px;display: flex;">
      <img v-if="dialogShow" src="@/assets/Group 1780.png" alt="" srcset=""
        style="width: 20px;height: 20px;margin-left: 10px;" @click="changeTongyi">
      <img v-else src="@/assets/homr/cirelin.png" alt="" srcset="" style="width: 20px;height: 20px;margin-left: 10px;"
        @click="changeTongyi">
      <div style="margin-left: 9px;margin-top: 2px;">点击确认已查看<span style="color: #389138;"
          @click="viewUserxieyi">《用户协议》</span>和<span style="color: #389138;" @click="viewyinsi">《隐私政策》</span></div>
    </div>
  </div>
</template>

<script>
import { Toast, Dialog } from 'vant';
import { send } from '@/api/user'
export default {
  name: 'Login',
  data() {
    return {
      form: {
        mobile: '',
        captcha: '',
        event: "mobilelogin",
        pid: ""

      },
      dialogShow: false,
      isclick: 1,
      loading: false,
      message: "获取验证码",
      seconds: 60,
      obginfo: []
    }
  },
  watch: {
    $route: {
      handler(route) {
        this.redirect = (route.query && route.query.redirect) || process.env.VUE_APP_BASE_PATH
      },
      immediate: true
    }
  },
  mounted() {
    console.log("~~~~~~~~1111~~~~~", window.location.href);

    this.obginfo = this.extractNumbers(window.location.href);
    console.log("~~~~~~~222222~~~~~~", this.obginfo[0]);
  },
  methods: {
    extractNumbers(str) {
      // 使用正则表达式匹配所有数字
      return str.match(/\d+/g) || [];
    },
    changeTongyi() {
      this.dialogShow = !this.dialogShow
    },
    //查看用户协议
    viewUserxieyi() {
      this.$router.push({
        path: '/xieyi'
      })
    },
    viewyinsi() {
      this.$router.push({
        path: '/Privacy'
      })
    },
    onClickLeft() {
      this.$router.back()
    },
    //获取验证码
    getCaptcha() {
      //
      this.isclick = 2
      send(this.form).then((res) => {
        if (res.code == 1) {
          this.daojshi()
        } else {
          Toast(res.msg)
        }

      })
    },
    daojshi() {
      let that = this
      const interval = setInterval(function () {
        if (that.seconds === 0) {
          clearInterval(interval);
          that.isclick = 1
          that.message = "重新获取验证码"
        } else {
          that.seconds--;
        }
      }, 1000);
    },
    // 校检手机号
    checkPhone(num) {
      if (num === 123456789) return true
      const reg = /^[1][3,4,5,7,8][0-9]{9}$/
      if (reg.test(num)) {
        return true
      }
      return false
    },
    // 提交
    onSubmit() {
      if (this.form.mobile == "") {
        this.$toast.fail("请输入手机号")
        return
      }
      if (this.form.captcha == "") {
        this.$toast.fail("请输入验证码")
        return
      }
      if (!this.dialogShow) {
        this.$toast.fail("请先同意用户协议和隐私政策")
        return
      }
      if (this.obginfo.length > 0) {
        this.form.pid = this.obginfo[0]
      }
      this.form.mobile = this.form.mobile.replace(/\s/g, "");
      this.loading = true
      this.$store
        .dispatch('user/login', this.form)
        .then(() => {
          this.$router.push({
            path: '/'
          })

        })
        .catch((e) => {
          this.$toast.fail(e.message)
        })
        .finally(() => {
          this.loading = false
        })
      // if (!this.dialogShow) {
      //   Dialog.confirm({
      //     title: '',
      //     message: '是否同意用户协议和隐私政策',
      //   })
      //     .then(() => {
      //       this.dialogShow = true
      //       this.loading = true
      //       this.$store
      //         .dispatch('user/login', this.form)
      //         .then(() => {
      //           this.$router.push({
      //             path: '/'
      //           })

      //         })
      //         .catch((e) => {
      //           this.$toast.fail(e.message)
      //         })
      //         .finally(() => {
      //           this.loading = false
      //         })
      //     })
      //     .catch(() => {
      //       // on cancel
      //     });
      // } else {
      //   this.loading = true
      //   this.$store
      //     .dispatch('user/login', this.form)
      //     .then(() => {
      //       this.$router.push({
      //         path: '/'
      //       })

      //     })
      //     .catch((e) => {
      //       this.$toast.fail(e.message)
      //     })
      //     .finally(() => {
      //       this.loading = false
      //     })
      // }


    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  .header {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

    .header__logo {
      display: block;
      width: 100%;
      width: 301px;
      height: 272px;
      margin-top: 59px;
      // height: 800px;
    }

    .header__title {
      font-size: 36px;
      color: #000;
      font-weight: normal;
      padding-top: 30px;
    }
  }

  .form {
    // background: red;
    padding: 24px;
    // margin-top: 50px;
  }


}

.van-nav-bar__title::v-deep {
  font-family: MiSans, MiSans;
  font-weight: 400;
  font-size: 46px !important;
  color: #151515;
}
</style>
